package com.medical.goods.service.impl;

import com.medical.goods.entity.SeckillSessionEntity;
import com.medical.goods.service.SeckillSessionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.medical.common.utils.PageUtils;
import com.medical.common.utils.Query;

import com.medical.goods.dao.GoodsSeckillRelationDao;
import com.medical.goods.entity.GoodsSeckillRelationEntity;
import com.medical.goods.service.GoodsSeckillRelationService;


@Service("goodsSeckillRelationService")
public class GoodsSeckillRelationServiceImpl extends ServiceImpl<GoodsSeckillRelationDao, GoodsSeckillRelationEntity> implements GoodsSeckillRelationService {

    @Autowired
    private SeckillSessionService seckillSessionService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<GoodsSeckillRelationEntity> page = this.page(
                new Query<GoodsSeckillRelationEntity>().getPage(params),
                new QueryWrapper<GoodsSeckillRelationEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 获取两天内所有商品的信息
     * @return
     */

    @Override
    public List<SeckillSessionEntity> getSession2daysGoodsId() {
        //查找对应时间端的活动
        List<SeckillSessionEntity> sessionEntities = seckillSessionService.list(new QueryWrapper<SeckillSessionEntity>().between("start_time", start(), end()));
        if(sessionEntities.size()>0&&sessionEntities!=null){
            //将活动对应的秒杀商品存入活动里
            for (SeckillSessionEntity sessionEntity : sessionEntities) {
                List<GoodsSeckillRelationEntity> session = baseMapper.selectList(new QueryWrapper<GoodsSeckillRelationEntity>().eq("session_id", sessionEntity.getId()));
                sessionEntity.setRelationEntities(session);
            }
            return sessionEntities;
        }
        return null;
    }

    /**
     * 获取当前时间
     * @return
     */
    public String start(){
        LocalDate now = LocalDate.now();
        LocalTime min = LocalTime.MIN;//00:00:00
        LocalDateTime start = LocalDateTime.of(now, min);
        //格式化开始时间
        String format = start.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        return format;
    }

    /**
     * 获取两天后的时间
     * @return
     */
    public String end(){
        LocalDate now = LocalDate.now();
        LocalDate localDate = now.plusDays(2);
        LocalTime max = LocalTime.MAX;//23:59:59
        LocalDateTime end = LocalDateTime.of(localDate, max);
        String format = end.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        return format;
    }
}